package com.navi168.fwq21.monitoring;

import java.util.ArrayList;
import java.util.Random;

import com.amap.mapapi.core.GeoPoint;
import com.amap.mapapi.location.LocationManagerProxy;
import com.amap.mapapi.map.MapActivity;
import com.amap.mapapi.map.MapController;
import com.amap.mapapi.map.MapView;
import com.amap.mapapi.map.Overlay;
import com.navi168.fwq21.R;
import com.navi168.fwq21.hyzl.MyLocationOverlayProxy;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Monit extends MapActivity {

	private static final String SPF = "NAVI168";
	private Button back;
	private Button refresh;
	private boolean gps_flag;// 可否定位开关
	private Context context;
	private GeoPoint point;
	private MapView mapView;
	private MapController mMapController;
	private ArrayList<GeoPoint> points = new ArrayList<GeoPoint>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.monit_map);
		context = getApplicationContext();

		GeoPoint gPoint = new GeoPoint((int) ((31.18334 + 0.1) * 1E6),
				(int) ((121.43348 + 0.1) * 1E6));
		GeoPoint gPoint2 = new GeoPoint((int) ((31.18334 + 0.1) * 1E6),
				(int) ((121.43348 + 0.2) * 1E6));
		points.add(gPoint);
		points.add(gPoint2);

		findviews();
		setlisers();
		SharedPreferences settings = getSharedPreferences(SPF,
				MODE_WORLD_READABLE);
		gps_flag = settings.getBoolean("auto_loc", true);
		point = new GeoPoint(
				settings.getInt("city_lat", (int) (31.18334 * 1E6)),
				settings.getInt("city_long", (int) (121.43348 * 1E6)));
		mapView.setBuiltInZoomControls(true);
		mMapController = mapView.getController();
		mMapController.setCenter(point); // 设置地图中心点
		mMapController.setZoom(13);

	}

	private void setlisers() {
		refresh.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				int a = new Random().nextInt(2);
				Log.i("mylog", a+"a");
				
				Log.i("mylog", points.get(a).getLatitudeE6() + "gp.lat");
				if (mapView.getOverlays().size() > 0) {
					Log.i("mylog", mapView.getOverlays().size()
							+ "mapView.getOverlays().size()");
					mapView.getOverlays().remove(0);
				}

				mapView.getOverlays().add(new MyOverlay(context, points.get(a)));
				mMapController.setCenter(points.get(a));
			}
		});

	}

	private void findviews() {
		mapView = (MapView) findViewById(R.id.poisearch_MapView);
		back = (Button) findViewById(R.id.back);
		refresh = (Button) findViewById(R.id.refresh);

	}

	public class MyOverlay extends Overlay {
		private View popUpView;
		private LayoutInflater inflater;
		private Context context;
		private GeoPoint gPoint;

		public MyOverlay(Context context, GeoPoint gPoint) {
			this.context = context;
			inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			this.gPoint = gPoint;
		}

		@Override
		public void draw(Canvas canvas, MapView arg1, boolean arg2) {

			super.draw(canvas, arg1, arg2);
			Point screenPts = new Point();
			mapView.getProjection().toPixels(gPoint, screenPts);
			Bitmap bmp = BitmapFactory.decodeResource(getResources(),
					R.drawable.da_marker_red);
			canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);

		}

		@Override
		public boolean onTap(GeoPoint geo, final MapView view) {
			// TODO Auto-generated method stub
			if (popUpView != null) {
				view.removeView(popUpView);
			}
			popUpView = inflater.inflate(R.layout.popup2, null);
			TextView textView = (TextView) popUpView.findViewById(R.id.PoiName);
			TextView adressTextView = (TextView) popUpView
					.findViewById(R.id.PoiAddress);
			textView.setText("设备状态：离线\nNavi168状态：运行");
			adressTextView.setText("时间2012-02-02 00：00：00\n速度：15km");
			MapView.LayoutParams lp;
			lp = new MapView.LayoutParams(MapView.LayoutParams.WRAP_CONTENT,
					MapView.LayoutParams.WRAP_CONTENT, gPoint, 15, -50,
					MapView.LayoutParams.BOTTOM_CENTER);
			view.addView(popUpView, lp);
			popUpView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					view.removeView(popUpView);
				}
			});
			return super.onTap(geo, view);
		}

	}

}
